package myPetStore.web.servlet;

import myPetStore.domain.LineItem;
import myPetStore.domain.Order;
import myPetStore.persistence.impl.LineItemDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ViewOrderServlet extends HttpServlet {
    private static final String  VIEW_ORDER="/WEB-INF/jsp/order/viewOrder.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Order order = null;
        LineItemDaoImpl lineItemDao=new LineItemDaoImpl();
        HttpSession session=req.getSession();
        int orderId= Integer.parseInt(req.getParameter("orderId"));
        List<Order> orderList= (List<Order>) session.getAttribute("orderList");
        for(int i=0;i<orderList.size();i++){
            if(orderId==orderList.get(i).getOrderId()){
                order=orderList.get(i);
                List<LineItem> lineItemList=new ArrayList<>();
                lineItemList=lineItemDao.getLineItemsByOrderId(orderId);
                order.setLineItems(lineItemList);
                break;
            }
        }
        session.setAttribute("order",order);
        req.getRequestDispatcher(VIEW_ORDER).forward(req,resp);
    }
}
